package first.demo;
import java.util.Scanner;
import Jama.Matrix;

public class matrix {
    public static void main(String[] args) {
        int values = 100;
        double a[][] = new double[values][values];
        double b[][] =new  double[values][values];
        System.out.print("创建矩阵a:");
        a=matrix_create();
        System.out.print("创建矩阵b:");
        b=matrix_create();
        matrix_sum(a, b);
        matrix_plus(a, b);
    }
    //创建一个二维矩阵
    public static double[][] matrix_create() {
        int i = 0, j = 0,row,col;
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.print("输入矩阵的行:");
            row = input.nextInt();
            System.out.print("输入矩阵的列:");
            col = input.nextInt();
            if(row==col)
                break;
            if(row!=col)
                System.out.println("请重新输入!");
        }
        double a[][] = new double[row][col];
        for (i = 0; i < row; i++)
            for (j = 0; j < col; j++) {
                System.out.print("输入矩阵的值:");
                a[i][j] = input.nextInt();
            }
        return a;
    }
    //求两个矩阵相加
    public static void matrix_sum(double a[][],double b[][]){
        Matrix A = new Matrix(a);
        Matrix B = new Matrix(b);
        Matrix D = A.plus(B);
        System.out.print("打印矩阵A与矩阵B相加后的矩阵:");
        D.print(3,1);
    }
    //求两个矩阵相乘
    public static void matrix_plus(double a[][],double b[][]){
        Matrix A = new Matrix(a);
        Matrix B = new Matrix(b);
        Matrix D = A.times(B);
        System.out.print("打印矩阵A与矩阵B相乘后的矩阵:");
        D.print(3,1);
    }
}



